// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <vector>
#include <turbo/log/logging.h>
#include <cantor/function/function_caller.h>
#include <turbo/utility/status.h>
#include <cantor/function/registry.h>
#include <cantor/function/type_traits.h>
#include <cantor/function/signature.h>

namespace cantor {
    class ScalarFunctionBuilder {
    public:
        ScalarFunctionBuilder(FunctionRegistry *registry) : _registry(registry) {
        }

        ~ScalarFunctionBuilder() = default;

        template<typename R, typename A1, typename F>
        turbo::Status register_unary_scalar_function(std::string_view func_name,
                                                      const std::vector<std::string> &names = {},
                                                      std::string_view description = "") {
            auto r_type = CppTypeToTypeInfo<R>::convert();
            auto args = cpp_type_infos<A1>();
            std::shared_ptr<FunctionCaller> caller = std::make_shared<FunctionCaller>(
                &scalar_unary_executor<R, A1, F>);
            auto fd = make_function_def(std::move(r_type), std::move(args), func_name, names, description);
            TURBO_RETURN_NOT_OK(make_function_signature(fd.get()));
            return _registry->register_function(fd, std::move(caller));
        }


        template<typename R, typename A1, typename A2, typename F>
        turbo::Status register_binary_scalar_function(std::string_view func_name,
                                                      const std::vector<std::string> &names = {},
                                                      std::string_view description = "") {
            auto r_type = CppTypeToTypeInfo<R>::convert();
            auto args = cpp_type_infos<A1, A2>();
            std::shared_ptr<FunctionCaller> caller = std::make_shared<FunctionCaller>(
                &scalar_binary_executor<R, A1, A2, F>);
            auto fd = make_function_def(std::move(r_type), std::move(args), func_name, names, description);
            TURBO_RETURN_NOT_OK(make_function_signature(fd.get()));
            return _registry->register_function(fd, std::move(caller));
        }

        template<typename R, typename A1, typename A2, typename A3, typename F>
        turbo::Status register_ternary_scalar_function(std::string_view func_name,
                                                       const std::vector<std::string> &names = {},
                                                       std::string_view description = "") {
            auto r_type = CppTypeToTypeInfo<R>::convert();
            auto args = cpp_type_infos<A1, A2, A3>();
            std::shared_ptr<FunctionCaller> caller = std::make_shared<FunctionCaller>(
                &scalar_ternary_executor<R, A1, A2, A3, F>);
            auto fd = make_function_def(std::move(r_type), std::move(args), func_name, names, description);
            TURBO_RETURN_NOT_OK(make_function_signature(fd.get()));
            return _registry->register_function(fd, std::move(caller));
        }

        template<typename R, typename A1, typename A2, typename A3, typename A4, typename F>
        turbo::Status register_quaternary_scalar_function(std::string_view func_name,
                                                          const std::vector<std::string> &names = {},
                                                          std::string_view description = "") {
            auto r_type = CppTypeToTypeInfo<R>::convert();
            auto args = cpp_type_infos<A1, A2, A3, A4>();
            std::shared_ptr<FunctionCaller> caller = std::make_shared<FunctionCaller>(
                &scalar_quaternary_executor<R, A1, A2, A3, A4, F>);
            auto fd = make_function_def(std::move(r_type), std::move(args), func_name, names, description);
            TURBO_RETURN_NOT_OK(make_function_signature(fd.get()));
            return _registry->register_function(fd, std::move(caller));
        }

        template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename F>
      turbo::Status register_quinary_scalar_function(std::string_view func_name,
                                                        const std::vector<std::string> &names = {},
                                                        std::string_view description = "") {
            auto r_type = CppTypeToTypeInfo<R>::convert();
            auto args = cpp_type_infos<A1, A2, A3, A4, A5>();
            std::shared_ptr<FunctionCaller> caller = std::make_shared<FunctionCaller>(
                &scalar_quinary_executor<R, A1, A2, A3, A4, A5, F>);
            auto fd = make_function_def(std::move(r_type), std::move(args), func_name, names, description);
            TURBO_RETURN_NOT_OK(make_function_signature(fd.get()));
            return _registry->register_function(fd, std::move(caller));
        }


        template<typename R, typename... ARG>
        turbo::Status register_scalar_function(std::string_view func_name,
                                               const std::shared_ptr<FunctionCaller> &caller,
                                               const std::vector<std::string> &names = {},
                                               std::string_view description = "") {
            auto r_type = CppTypeToTypeInfo<R>::convert();
            auto args = cpp_type_infos<ARG...>();
            auto fd = make_function_def(std::move(r_type), std::move(args), func_name, names, description);
            TURBO_RETURN_NOT_OK(make_function_signature(fd.get()));
            return _registry->register_function(fd, std::move(caller));
        }

        template<typename R, typename... ARG>
        turbo::Status register_scalar_vector_function(std::string_view func_name,
                                                      const std::shared_ptr<FunctionCaller> &caller,
                                                      const std::vector<std::string> &names = {},
                                                      std::string_view description = "") {
            auto r_type = CppTypeToTypeInfo<R>::convert();
            auto args = cpp_type_infos<ARG...>();
            auto fd = make_simd_function_def(std::move(r_type), std::move(args), func_name, names, description);
            TURBO_RETURN_NOT_OK(make_function_signature(fd.get()));
            return _registry->register_function(fd, std::move(caller));
        }

        template<typename R, typename... ARG>
        turbo::Status register_scalar_gpu_function(std::string_view func_name,
                                                   const std::shared_ptr<FunctionCaller> &caller,
                                                   const std::vector<std::string> &names = {},
                                                   std::string_view description = "") {
            auto r_type = CppTypeToTypeInfo<R>::convert();
            auto args = cpp_type_infos<ARG...>();
            auto fd = make_gpu_function_def(std::move(r_type), std::move(args), func_name, names, description);
            TURBO_RETURN_NOT_OK(make_function_signature(fd.get()));
            return _registry->register_function(fd, std::move(caller));
        }

    private:
        std::shared_ptr<cantor::pb::catalog::FunctionDefine> make_function_def_impl(pb::TypeInfo &&ret, std::vector<pb::TypeInfo> &&args,
                                                                          cantor::pb::catalog::FunctionFeature f,
                                                                          std::string_view func_name,
                                                                          const std::vector<std::string> &names,
                                                                          std::string_view description);

        std::shared_ptr<cantor::pb::catalog::FunctionDefine> make_function_def(pb::TypeInfo &&ret, std::vector<pb::TypeInfo> &&args,
                                                                     std::string_view func_name,
                                                                     const std::vector<std::string> &names,
                                                                     std::string_view description) {
            return make_function_def_impl(std::move(ret), std::move(args),
                                          cantor::pb::catalog::FunctionFeature::FUNC_FEATURE_NONE, func_name, names,
                                          description);
        }

        std::shared_ptr<cantor::pb::catalog::FunctionDefine> make_simd_function_def(pb::TypeInfo &&ret, std::vector<pb::TypeInfo> &&args,
                                                                          std::string_view func_name,
                                                                          const std::vector<std::string> &names,
                                                                          std::string_view description) {
            return make_function_def_impl(std::move(ret), std::move(args),
                                          cantor::pb::catalog::FunctionFeature::FUNC_FEATURE_SIMD, func_name, names,
                                          description);
        }

        std::shared_ptr<cantor::pb::catalog::FunctionDefine> make_gpu_function_def(pb::TypeInfo &&ret, std::vector<pb::TypeInfo> &&args,
                                                                         std::string_view func_name,
                                                                         const std::vector<std::string> &names,
                                                                         std::string_view description) {
            return make_function_def_impl(std::move(ret), std::move(args),
                                          cantor::pb::catalog::FunctionFeature::FUNC_FEATURE_GPU, func_name, names,
                                          description);
        }

    private:
        FunctionRegistry *_registry{nullptr};
    };
} // namespace cantor
